package com.aiyige.page.edituserinfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.api.ApiManager;
import com.aiyige.base.api.ResponseCode;
import com.aiyige.model.User;
import com.aiyige.page.edituserinfo.adapter.EditUserAddressDetailChinaAdapter;
import com.aiyige.page.edituserinfo.model.Province;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ErrorUtil;
import com.aiyige.utils.RetrofitUtil;
import com.aiyige.utils.ToastX;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditUserAddressDetailChinaPage extends AppCompatActivity {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.mainRv)
    RecyclerView mainRv;
    Province province = null;
    EditUserAddressDetailChinaAdapter editUserAddressDetailChinaAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_address_detail_page);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        try {
            province = JSON.parseObject(intent.getStringExtra(EditUserAddressSubChinaPage.EXTRA_KEY_PROVINCE), Province.class);
            if (province == null) {
                throw new Exception("数据异常");
            }
        } catch (Exception e) {
            ToastX.show(e.getMessage());
            finish();
        }
        titleTv.setText(province.getName());
        editUserAddressDetailChinaAdapter = new EditUserAddressDetailChinaAdapter();
        mainRv.setLayoutManager(new LinearLayoutManager(this));
        mainRv.setAdapter(editUserAddressDetailChinaAdapter);
        editUserAddressDetailChinaAdapter.setNewData(province.getCity());
        editUserAddressDetailChinaAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ApiManager.getService()
                        .userCenterUsers(String.valueOf(AccountUtil.getCurrentUser().getId()), User.newBuilder().moreBackup(User.MoreBackup.newBuilder().city(editUserAddressDetailChinaAdapter.getItem(position).getId()).build()).build())
                        .enqueue(new Callback<ResponseBody>() {
                            @Override
                            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                try {
                                    if (response.code() != ResponseCode.CREATED) {
                                        throw new Exception(ErrorUtil.extractMessageFromErrorResponse(response));
                                    }
                                    String json = RetrofitUtil.getResponseBodyAsString(response);
                                    User tempUser = JSON.parseObject(json, User.class);
                                    User user = AccountUtil.getCurrentUser();
                                    user.setMoreBackup(tempUser.getMoreBackup());
                                    AccountUtil.updateCurrentUser(user);
                                    ToastX.show(R.string.edit_user_address_success);
                                    setResult(RESULT_OK);
                                    finish();
                                } catch (Exception e) {
                                    onFailure(call, e);
                                }
                            }

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {
                                ToastX.show(t.getMessage());
                            }
                        });
            }
        });
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                setResult(RESULT_CANCELED);
                finish();
                break;
        }
    }
}
